package logic;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Course {
	String id;
	String name;
	String degreeId;

	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getDegreeId() {
		return degreeId;
	}

	public Course(String id, String name, String degreeId) {
		super();
		this.id = id;
		this.name = name;
		this.degreeId = degreeId;
	}

	public static List<Course> getCoursesFromJSON(String json) {
		ArrayList<Course> courses = new ArrayList<Course>();
		try {
			JSONArray jsonArray = new JSONArray(json);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				String id = jsonObject.getString("id_asig");
				String name = jsonObject.getString("nombre");
				String degree = jsonObject.getString("id_carrera");

				courses.add(new Course(id, name, degree));
			}
			return courses;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}
}
